package com.edu.controller;


import com.edu.entity.Category;
import com.edu.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 文章分类表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-05-15
 */
@Controller
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    @GetMapping("")
    @PreAuthorize("hasRole('admin')")
    public String index(Model model){
        log.info("进入分类列表");
        List<Category> list = categoryService.list();
        model.addAttribute("list",list);
        return "category";
    }

    @GetMapping("/add")
    public String showAddForm() {
        return "category-add"; // 新增页面
    }
    @PostMapping("/add")
    public String addCategory(Category category) {
        categoryService.save(category);
        return "redirect:/category"; // 添加后跳回列表页
    }

    @GetMapping("/delete/{id}")
    public String deleteCategory(@PathVariable Long id) {
        categoryService.removeById(id);
        return "redirect:/category"; // 删除后跳回列表页
    }



}
